#!/bin/bash
TOOLS="$HOME/.dotnet/tools"
if [[ ":$PATH:" != *":$TOOLS:"* ]]; then
    export PATH="$PATH:$TOOLS"
fi

# 项目路径（根据实际情况调整）
DATA_PROJECT="StudentVideo.Data/StudentVideo.Data.csproj"
STARTUP_PROJECT="StudentVideo/StudentVideo.csproj"

if [[ -n "$1" ]]; then
    echo "Rolling back to migration: $1"
    if ! dotnet ef database update "$1" \
        --project "$DATA_PROJECT" \
        --startup-project "$STARTUP_PROJECT"; then
        echo "Error: Rollback failed" >&2
        exit 1
    fi
    echo "Rollback completed successfully"
else
    echo "Applying the latest migration..."
    if ! dotnet ef database update \
        --project "$DATA_PROJECT" \
        --startup-project "$STARTUP_PROJECT"; then
        echo "Error: Database update failed" >&2
        exit 1
    fi
    echo "Database updated successfully"
fi

exit 0
